package cn.pconline.autoclub.service;

import java.util.Date;
import java.util.List;

import org.gelivable.dao.GeliDao;
import org.gelivable.dao.GeliOrm;
import org.gelivable.dao.SqlBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;

import cn.pconline.autoclub.entity.InviteJoin;
import cn.pconline.autoclub.util.DateUtils;

/**
 * 一键邀请service
 * @author fengguosen
 */
public class InviteJoinService {
	private static final Logger log = LoggerFactory.getLogger(InviteJoinService.class);
    @Autowired
    private GeliOrm geliOrm;
    @Autowired
    private GeliDao geliDao;
   
    
    
    public long createInviteJoin(InviteJoin entity){
    	return geliDao.create( entity );
    }
    
    /**
     * 查找一键推荐记录
     * @param code
     * @return
     */
    public InviteJoin getInviteJoinByCode(String code){
    	String nowDate = DateUtils.formatDetail(new Date());
    	SqlBuilder sql = new SqlBuilder();
    	sql.appendSql(" select invite_id from ").appendSql(geliOrm.getTableName(InviteJoin.class)).appendSql(" where 1=1 ");
    	sql.appendSql(" and invite_code = ").appendValue(code);
    	sql.appendSql(" and invited_count <").appendValue(InviteJoin.INVITE_JOIN_COUNT);
    	sql.appendSql(" and overdue_at >").appendValue(nowDate);
    	try {
    		List<InviteJoin> list = geliDao.list( InviteJoin.class, sql.getSql(), sql.getValues());
    		if(list!=null&&list.size()>0) return list.get(0);
        }
        catch( DataAccessException e ) {
        	log.info("the code {} is not vaild in inviteJoin",code);
	        return null;
        }
    	return null;
    }
      /**
     * 获取未过期车友会邀请URL
     * @param code
     * @return
     */
    public InviteJoin getInviteUrlByClubId(long clubId){
    	String nowDate = DateUtils.formatDetail(new Date());
    	SqlBuilder sql = new SqlBuilder();
    	sql.appendSql(" select invite_id from ").appendSql(geliOrm.getTableName(InviteJoin.class)).appendSql(" where 1=1 ");
    	sql.appendSql(" and club_id = ").appendValue(clubId);
    	sql.appendSql(" and invited_count <").appendValue(InviteJoin.INVITE_JOIN_COUNT);
    	sql.appendSql(" and overdue_at >").appendValue(nowDate);
    	try {
    		List<InviteJoin> list = geliDao.list( InviteJoin.class, sql.getSql(), sql.getValues());
    		if(list!=null&&list.size()>0) return list.get(0);
        }
        catch( DataAccessException e ) {
        	log.info("the code {} is not vaild in inviteJoin",clubId);
	        return null;
        }
    	return null;
    }
    public int updateInviteJoinCount(InviteJoin inviteJoin){
        if(inviteJoin==null){
            return 0;
        }
        return geliDao.update(inviteJoin,"invitedCount");
    }
    /**
     * 查找一键推荐记录(无论是否过期 )
     * @param code
     * @return
     */
    public InviteJoin getAllInviteJoinByCode(String code){
    	String nowDate = DateUtils.formatDetail(new Date());
    	SqlBuilder sql = new SqlBuilder();
    	sql.appendSql(" select invite_id from ").appendSql(geliOrm.getTableName(InviteJoin.class)).appendSql(" where 1=1 ");
    	sql.appendSql(" and invite_code = ").appendValue(code);
    	try {
    		List<InviteJoin> list = geliDao.list( InviteJoin.class, sql.getSql(), sql.getValues());
    		if(list!=null&&list.size()>0) return list.get(0);
        }
        catch( DataAccessException e ) {
        	log.info("the code {} is not vaild in inviteJoin",code);
	        return null;
        }
    	return null;
    }
        
}
